package com.lzj.demo.collection.list;

import com.lzj.demo.dto.Student;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        List<Student> list = init();
        listByIterator(list);
        listByFor(list);
        listByForStrong(list);
    }

    /**
     * 初始化
     **/
    public static List<Student> init(){
        List<Student> list = new ArrayList<>();
        Student s1 = new Student(1L,"林泽健",20);
        Student s2 = new Student(2L,"规范化",21);
        Student s3 = new Student(3L,"京津冀",26);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        return list;
    }

    /**
     * 迭代器方式遍历
     * */
    public static void listByIterator(List<Student> list){
        Iterator<Student> it = list.iterator();
        while (it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+","+s.getAge());
        }
    }

    /**
     * 遍历方式for循环带索引的
     * */
    public static void listByFor(List<Student> list){
        for(int i=0;i< list.size();i++){
            Student s = list.get(i);
            System.out.println(s.getName()+","+s.getAge());
        }
    }

    /**
     * 增强for方式
     * */
    public static void listByForStrong(List<Student> list){
        for(Student s:list){
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
